<template>
  <qx-form ref="AttrsForm" v-model="formInfo" :option="formOption" class="q-mr-md"></qx-form>
</template>

<script setup>
import { i18n } from 'boot/i18n'
import { postAPI, putAPI } from 'src/api'
import { getRemoteData } from 'src/enum/ajaxRemoteData.js'
import { directional } from 'src/views/CarrierCore/enum.js'
import {
  formatRackDirections, IsAvoidable, IsRotatable, NavigationTypes, Quadrants, stationDirections
} from 'src/views/MapCore/enum.js'
import { computed, ref, watch } from 'vue'

const props = defineProps({ rowInfo: { type: Object, default: () => ({}) } })
const modalVisible = defineModel('modelValue', { type: Boolean, default: false })

const racks = ref([])
const formInfo = ref({})
const AGVModels = ref([])
const AttrsForm = ref(null)
getRemoteData('agv_model', '/yfagv/api/agvTypeDevice/list').then(data => {
  AGVModels.value = [{ deviceName: i18n.t('All'), deviceCode: '-1' }, ...data]
})
getRemoteData('agv_rack', '/yfagv/api/agvMasDataShelfBase/shelfTypeList').then(data => {
  racks.value = [{ originalLabel: i18n.t('All'), label: '-1' }, ...data]
})
const filterIsRotatable = computed(() => IsRotatable.value.filter(el => el.value !== '2'))
const filterDirectional = computed(() => directional.value.filter(el => el.value !== '-1'))
const formOption = computed(() => {
  return {
    labelWidth: '120', span: 12,
    column: [{
      label: `${i18n.t('StationAttrs.RoadBack')}`, field: 'roadProperty', type: 'switch', falseValue: '0',
      trueValue: '1', value: '1', class: 'custom-switch'
    }, {
      label: `${i18n.t('CarrierCore.RackDirection')}`, field: 'allDir', type: 'radio',
      options: formatRackDirections.value, value: '9'
    }, {
      label: `${i18n.t('StationAttrs.SiteDirection')}`, field: 'eleDir', type: 'radio',
      options: stationDirections.value, value: '999'
    }, {
      label: `${i18n.t('StationAttrs.BordEntryDirection')}`, field: 'agvInDir', required: true
    }, {
      label: `${i18n.t('StationAttrs.RotateAvoidance')}`, field: 'rotarySwitch', type: 'switch', falseValue: '0',
      trueValue: '1', value: '0', class: 'custom-switch'
    }, {
      label: `${i18n.t('StationAttrs.RotateAvoidanceRadius')}(mm)`, field: 'rotRad', type: 'number', value: '0',
      decimals: 0, min: -1000, max: 3000, display: formInfo.value.rotarySwitch === '1', required: true
    }, {
      label: `${i18n.t('StationAttrs.AllowedVehicleType')}`, field: 'roBotType', type: 'select', multiple: true,
      value: '-1', options: AGVModels.value, props: { label: 'deviceName', value: 'deviceCode' }, required: true
    }, {
      label: `${i18n.t('StationAttrs.RackUnderRotation')}`, field: 'rotUnderPod', type: 'select',
      options: IsRotatable.value, value: '0', required: true
    }, {
      label: `${i18n.t('StationAttrs.LiftingPlate')}`, field: 'rotMech', type: 'radio',
      options: filterIsRotatable.value, value: '0'
    }, {
      label: `${i18n.t('StationAttrs.IsAvoidable')}`, field: 'evit', type: 'select',
      options: IsAvoidable.value, value: '0', required: true
    }, {
      label: `${i18n.t('StationAttrs.RotatingModel')}`, field: 'rotByRoBotType', type: 'select', multiple: true,
      value: '-1', options: AGVModels.value, props: { label: 'deviceName', value: 'deviceCode' }, required: true
    }, {
      label: `${i18n.t('StationAttrs.RotatingRack')}`, field: 'rotForPodType', type: 'select', multiple: true,
      value: '-1', options: racks.value, props: { label: 'originalLabel', value: 'label' }, required: true
    }, {
      label: `${i18n.t('StationAttrs.PalletRecognition')}`, field: 'palletRecogniTion', type: 'switch',
      falseValue: '0', trueValue: '1', value: '0', class: 'custom-switch'
    }, {
      label: `${i18n.t('StationAttrs.RotatingObstacleZone')}`, field: 'rotBarrierArea', type: 'select',
      multiple: true, options: Quadrants.value, required: true
    }, {
      label: `${i18n.t('MapCore.RefreshMap')}`, field: 'isUpdateMap', type: 'switch', falseValue: '0',
      trueValue: '1', value: '1', class: 'custom-switch'
    }, {
      label: `${i18n.t('StationAttrs.CTUForwardDocking')}`, field: 'ctuStance', type: 'switch', falseValue: '0',
      trueValue: '1', value: '1', class: 'custom-switch'
    }, {
      label: `${i18n.t('StationAttrs.IsPause')}`, field: 'tranZoneType', type: 'switch',
      falseValue: '0', trueValue: '1', value: '0', class: 'custom-switch'
    }, {
      label: `${i18n.t('StationAttrs.PausePriority')}`, field: 'priority', type: 'number', decimals: 0,
      min: 1, max: 127, required: true, display: formInfo.value.tranZoneType === '1'
    }, {
      label: `${i18n.t('StationAttrs.LinkedForkliftWaitPoint')}`, field: 'forkWaitPos', required: true
    }, {
      label: `${i18n.t('StationAttrs.LeaveCurrentPointDistance')}`, field: 'outFromBinDis', type: 'number',
      value: '0', decimals: 0, min: 0, max: 10000, required: true,
      display: ['22'].includes(formInfo.value.positionPackageCode)
    }, {
      label: `${i18n.t('AGVCore.NavigationType')}`, field: 'mapCrossType', type: "radio",
      options: NavigationTypes.value, value: '0',
      display: ['26'].includes(formInfo.value.positionPackageCode)
    }, {
      label: `${i18n.t('StationAttrs.HandoverCodeType')}`, field: 'qrCode', required: true,
      display: ['26'].includes(formInfo.value.positionPackageCode)
    }, {
      label: `${i18n.t('StationAttrs.HandoverXCoord')}(m)`, field: 'xposNew', type: 'number', value: '0', decimals: 3,
      min: 0, max: 1000, required: true, display: ['26'].includes(formInfo.value.positionPackageCode)
    }, {
      label: `${i18n.t('StationAttrs.HandoverYCoord')}(m)`, field: 'yposNew', type: 'number', value: '0', decimals: 3,
      min: 0, max: 1000, required: true, display: ['26'].includes(formInfo.value.positionPackageCode)
    }, {
      label: `${i18n.t('StationAttrs.HandoverCoordOffset')}`, field: 'ncoor', type: 'radio', value: '0',
      options: filterDirectional.value, display: ['26'].includes(formInfo.value.positionPackageCode)
    }, {
      label: `${i18n.t('StationAttrs.XCodeSpacing')}`, field: 'stepXpos', type: 'number', decimals: 0, min: 0,
      max: 9999, display: ['33'].includes(formInfo.value.positionPackageCode)
    }, {
      label: `${i18n.t('StationAttrs.YCodeSpacing')}`, field: 'stepYpos', type: 'number', decimals: 0, min: 0,
      max: 9999, display: ['33'].includes(formInfo.value.positionPackageCode)
    }]
  }
})

watch(modalVisible, val => val && (formInfo.value = props.rowInfo || {}), { immediate: true })
const submitAjax = mDone => {
  AttrsForm.value.validate((form, done) => {
    const API = form.id ? putAPI : postAPI
    if (form.roBotType.includes('-1')) form.roBotType = '-1'
    if (form.rotForPodType.includes('-1')) form.rotForPodType = '-1'
    if (form.rotByRoBotType.includes('-1')) form.rotByRoBotType = '-1'
    API('/yfagv/api/agvMapPointAttributeValue', form, () => {
      done()
      mDone && mDone()
    }, () => {
      done()
      mDone && mDone(false)
    })
  }, () => mDone && mDone(false))
}

defineExpose({ submitAjax })
</script>